package com.pi.pro.xcmg.am.was.web.cust.controller;



import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.was.fac.service.WasCustomWorkService;
import com.pi.wms.dto.act.PickTaskChangeActionDto;
import com.pi.wms.entity.TwaDocWaveHeader;
import com.pi.wms.entity.TwtActTaskList;
import com.pi.wms.service.AllocationService;
import com.pi.wms.service.TaskService;
import com.pi.wms.service.WaveDetailsService;
import com.pi.wms.service.WaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
@RequestMapping("/outbound/wave/ca")
@Slf4j
public class CustApplyWaveController extends BaseCrudController<TwaDocWaveHeader, Long> {

    @Autowired
    private WaveService service;

    @Autowired
    private WaveDetailsService detailsService;

    @Autowired
    private AllocationService allocationService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private WasCustomWorkService customWorkService;

    @Override
    protected ICrudService<TwaDocWaveHeader, Long> getCrudService() {
        return service;
    }

    @Override
    protected String getPrefix() {
        return "wave";
    }


    @Override
    protected String getRequestMapping() {
        return "/outbound/wave";
    }

    @PostMapping("prompt")
    public String prompt(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        //
        model.addAttribute("params", getParams(request));
        String m = "";
        TwaDocWaveHeader header = null;
        List<TwtActTaskList> tasks = null;

        if ((ids == null) || (ids.length == 0)) {
            addSuccessMessage(redirectAttributes, "提升优先级成功");
            return this.redirect("");
        }

        for (Long id : ids) {
            if (id == null) {
                continue;
            }
            m = this.customWorkService.promptWave(id, SecurityUtils.getLoginUsername());
            if (!StringUtils.isNullOrBlank(m)) {
                addErrorMessage(redirectAttributes, m);
                return this.redirect("");
            }
        }
        addSuccessMessage(redirectAttributes, "激活波次提交成功");
        return this.redirect("");
    }

    @PostMapping("pickstation")
    public String pickStation(@RequestParam("ids") Long[] ids,@RequestParam("pickstations") List<String> pickstations, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        //
        model.addAttribute("params", getParams(request));
        String m = "";
        TwaDocWaveHeader header = null;
        List<TwtActTaskList> tasks = null;

        if ((ids == null) || (ids.length == 0)) {
            addSuccessMessage(redirectAttributes, "设置拣货站台成功");
            return this.redirect("");
        }

        for (Long id : ids) {
            if (id == null) {
                continue;
            }
            m = this.customWorkService.setPickStation(id, pickstations, SecurityUtils.getLoginUsername());
            if (!StringUtils.isNullOrBlank(m)) {
                addErrorMessage(redirectAttributes, m);
                return this.redirect("");
            }
        }
        addSuccessMessage(redirectAttributes, "设置拣货站台成功");
        return this.redirect("");
    }

    @PostMapping("execute")
    public String active(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        //
        model.addAttribute("params", getParams(request));
        String m = "";
        TwaDocWaveHeader header = null;
        List<TwtActTaskList> tasks = null;

        if ((ids == null) || (ids.length == 0)) {
            addSuccessMessage(redirectAttributes, "执行波次提交成功");
            return this.redirect("");
        }

        for (Long id : ids) {
            if (id == null) {
                continue;
            }
            m = this.customWorkService.executeWave(id, SecurityUtils.getLoginUsername());
            if (!StringUtils.isNullOrBlank(m)) {
                addErrorMessage(redirectAttributes, m);
                return this.redirect("");
            }
        }
        addSuccessMessage(redirectAttributes, "执行波次提交成功");
        return this.redirect("");
    }


    @PostMapping("allocateso")
    public String allocateSo(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        //
        model.addAttribute("params", getParams(request));
        if ((ids == null) || (ids.length == 0)) {
            addSuccessMessage(redirectAttributes, "波次分波成功");
            return this.redirect("");
        }
        String m = "";
        PickTaskChangeActionDto actionDto=new PickTaskChangeActionDto();
        actionDto.setWorker(SecurityUtils.getLoginUsername());
        actionDto.setWorkTime(new Date());
        for (Long id : ids) {
            if (id == null) {
                continue;
            }
            //m = this.customWorkService.waveSoAllocateShip(id, actionDto);
            if (!StringUtils.isNullOrBlank(m)) {
                addErrorMessage(redirectAttributes, m);
                return this.redirect("");
            }
        }
        addSuccessMessage(redirectAttributes, "波次分波成功");
        return this.redirect("");
    }


    @Override
    protected String redirect(String relativeUrl) {
        if (!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return "redirect:" + this.referUrl() + relativeUrl;
    }

    private String referUrl() {
        return "/outbound/wave/";
    }

}
